package org.lmd.platform.config;

import com.google.common.collect.Lists;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.*;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


@Configuration
@EnableSwagger2
@ComponentScan(basePackages = {"org.lmd.platform.controller"})
@EnableWebMvc
public class SwaggerConfig extends WebMvcConfigurationSupport {

    // https://github.com/springfox/springfox/issues/2201
    // 解决页面显示验证失败，禁用验证。
    @Bean
    UiConfiguration uiConfig() {
        return UiConfigurationBuilder.builder()
                .displayRequestDuration(true)
                .validatorUrl("")
                .build();
    }

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo()).select()
                .apis(RequestHandlerSelectors.basePackage("org.lmd.platform.controller"))
                .paths(PathSelectors.any()).build()
                .securitySchemes(Lists.newArrayList(apiKey()));
    }

    private ApiInfo apiInfo() {
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("管理平台 Restful API文档")
                .description("测试用户：182；UserToken：\n" +
                        "06F46F8D5C646C3030ECFDC394C1D44E5FE6DA3C1092E8513C4A30F3EAD84FB9C28AED6B8271B98A0B7B009229383F3DB108BD6F2D4C4DAC64C306E9185509609101F5EC07231CACDD034645A6D092C4C59198AC119612DAA8BA398B50BBC675\n" +
                        "")
                .contact(new Contact("", "", ""))
                .version("1.0")
                .build();
        return apiInfo;
    }

    private ApiKey[] apiKey() {
        return new ApiKey[]{new ApiKey("UserToken", "UserToken", "header"),
                new ApiKey("DeviceToken", "DeviceToken", "header")};
    }


}